package com.uestc.hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;

import com.uestc.util.HBaseProperties;


public class HBaseUtil {
	private static Configuration conf;
	private static Connection hConnection;
	static {
		init();
	}
	
	private static void init() {
		conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum",
				HBaseProperties.getValue("hbase.zookeeper.quorum"));
		conf.set("hbase.zookeeper.property.clientPort",
				HBaseProperties.getValue("hbase.zookeeper.property.clientPort"));
		conf.set("hbase.rpc.protection",
				HBaseProperties.getValue("hbase.rpc.protection"));
		System.setProperty("hadoop.home.dir",
				HBaseProperties.getValue("hadoop.home"));
		try {
			hConnection = ConnectionFactory.createConnection(conf);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void reload() {
		init();
	}

	public static Configuration getConf() {
		if (conf == null)
			init();
		return conf;
	}
	
	public static Connection getCon(){
		if (hConnection == null)
			init();
		return hConnection;
	}

	public static Table getHTable(String tableName) throws IOException {
		return hConnection.getTable(TableName.valueOf(tableName));
	}
}
